package com.prs.crm.action.approval;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.approval.JobType;
import com.prs.crm.domain.approval.ShenQingType;
import com.prs.crm.service.approval.JobTypeService;

public class JobTypeAction extends BaseAction{
	private JobTypeService jobTypeService;
	private JobType jobType;
	private Collection<TreeNode> nodes;
	private PaginationSupport pagination;
	private Integer[] ids;
	private List<JobType> jobtypes;
	public JobTypeService getJobTypeService() {
		return jobTypeService;
	}
	public void setJobTypeService(JobTypeService jobTypeService) {
		this.jobTypeService = jobTypeService;
	}
	public Collection<TreeNode> getNodes() {
		return nodes;
	}
	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public JobType getJobType() {
		return jobType;
	}
	public void setJobType(JobType jobType) {
		this.jobType = jobType;
	}
	public List<JobType> getJobtypes() {
		return jobtypes;
	}
	public void setJobtypes(List<JobType> jobtypes) {
		this.jobtypes = jobtypes;
	}
	/*******************************************************************/
	//***************报表页树******************/
	@Action(value = "loadJobTypeTree", results = { @Result(name = "success", type = "json",
			params = {"root", "nodes" }) })
	public String loadJobTypeTree(){
		nodes = new ArrayList<TreeNode>();
		Collection<JobType> jobTypes = this.getJobTypeService().getRootJobTypes();
		for (JobType job : jobTypes) {
			TreeNode node = new TreeNode();
			node.setId(String.valueOf(job.getId()));
			node.setText(job.getName());
			addChildNode(job, node);
			nodes.add(node);
		}
		return SUCCESS;
	}
	private void addChildNode(JobType jobtype,TreeNode parentnode){
		if(jobtype.getChilds() != null){
			for(JobType job:jobtype.getChilds()){
				TreeNode node = new TreeNode();
				node.setId(String.valueOf(job.getId()));
				node.setText(job.getName());
				parentnode.addChildNode(node);
				addChildNode(job, node);
			}
		}
	}
	/**列表**/
	@Action("listJobtype")
	public String listJobtype(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(JobType.class)
										.add(Restrictions.isNull("parent"));
		this.setPagination(this.jobTypeService.listByPage(criteria, 
				this.getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	@Action("editJobType")
	public String editJobtype(){
		return SUCCESS;
	}
	@Action(value = "saveJobType", results = {
			@Result(name = "success", type = "redirect", location = "listJobtype"),
			@Result(name = "error", type = "chain", location = "editJobType")})
	public String saveJobtype(){
		if(jobType.getName() == null || "".equals(jobType.getName().trim())){
			addActionError("名称不能为空！");
			return ERROR;
		}
		this.jobTypeService.save(jobType);
		return SUCCESS;
	}
	@Action(value = "removeJobType", results = {
			@Result(name = "success", type = "redirect", location = "listJobtype"),
			@Result(name = "error", type = "chain", location = "listJobtype")})
	public String removeJobType(){
		if(ids == null){
			this.setPrompt("请选择要删除的数据行！");
			return ERROR;
		}
		this.jobTypeService.batchRemove(ids);
		return SUCCESS;
	}
	
}
